package com.cz.nongyetongv5.adapter;

import java.io.File;
import java.util.List;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView.ScaleType;

import com.cz.nongyetongv5.bean.Photo;
import com.cz.nongyetongv5.custom.photoview.PhotoView;
import com.cz.nongyetongv5.custom.photoview.PhotoViewAttacher.OnPhotoTapListener;

/**
 * 详细的照片的PagerAdapter
 *
 * @author admin
 *
 */
public class PhotoDetailPageAdapter extends PagerAdapter {
	/**
	 * 所有的数据集合
	 */
	private List<Photo> photos = null;

	/**
	 * 图片轻敲监听器
	 */
	private OnPhotoTapListener photoTapListener = null;

	public void setPhotos(List<Photo> photos) {
		this.photos = photos;

		this.notifyDataSetChanged();
	}

	/**
	 * 书写构造函数,将视图层传递进来
	 *
	 * @param views
	 */
	public PhotoDetailPageAdapter(List<Photo> photos,
								  OnPhotoTapListener photoTapListener) {
		this.photos = photos;
		this.photoTapListener = photoTapListener;
	}

	/**
	 * 官方提示这么写
	 */
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// 官方提示这么写
		return arg0 == arg1;
	}

	/**
	 * 返回页卡的数量
	 */
	@Override
	public int getCount() {
		return this.photos != null ? this.photos.size() : 0;
	}

	/**
	 * 销毁一个页卡
	 */
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	/**
	 * 获得元素的下标
	 */
	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	/**
	 * 获得Page标题
	 */
	@Override
	public CharSequence getPageTitle(int position) {
		return super.getPageTitle(position);
	}

	/**
	 * 实例化页卡
	 */
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		// 当前容器增加一个View,增加到第一个
		PhotoView photoView = new PhotoView(container.getContext());

		// 取出数据
		Photo photo = photos.get(position);

		// 获得大图的路径
		String bigPhotoPath = photo.getBigPhotoPath();

		File bigPhoto = new File(bigPhotoPath);

		if (bigPhoto.exists()) {

			BitmapFactory.Options options = new BitmapFactory.Options();

			options.inSampleSize = 2;

			Bitmap image = BitmapFactory.decodeFile(bigPhotoPath, options);

			photoView.setImageBitmap(image);

			photoView.setClickable(true);

			// photoView.setAdjustViewBounds(true) ;
			//
			// photoView.setScaleType(ScaleType.CENTER) ;

			photoView.setOnPhotoTapListener(photoTapListener);
		}

		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		container.addView(photoView, layoutParams);

		return photoView;
	}

}
